package com.wt.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wt.pojo.Distributor;
import com.wt.service.WelcomeService;
import com.wt.util.StringUtil;

/**
 * Author hiberk
 * Date 2017/6/26 17:55
 */
@Controller
public class WelcomeController {
	@Resource
	private WelcomeService welcomeService;
	
    @RequestMapping(value = "/index")
    public String index() throws Exception{
        return "login";
    }

    @RequestMapping(value = "/register")
    public String register() throws Exception{
        return "register";
    }
    
    @RequestMapping(value = "/template")
    public String template(HttpServletRequest request) {
        System.out.println(request.getContextPath() );
        return "/template/index";
    }

    @RequestMapping(value = "/origin")
    public String origin(HttpServletRequest request) {
        System.out.println(request.getContextPath() );
        return "/template/origin";
    }
    
    @RequestMapping(value = "/error")
    public String error(HttpServletRequest request, Model model,Integer msg) {
    	String error = "";
    	if(msg == 1){
    		error ="注册信息不全,请重新填写";
    	}else if(msg == 2){
    		error ="账号已被注册,请重新填写";
    	}else if(msg == 3){
    		error ="网络连接超时,请重试";
    	}else if(msg == 4){
    		error ="两次密码不一致,请重新输入";
    	}else if(msg == 5){
    		error = "账号未通过审核或已被冻结";
    	}
    	model.addAttribute("msg", error);
        return "/errorMsg";
    }
    /**
     * 注册成功跳转页面
     * @param request
     * @param model
     * @param msg
     * @return
     */
    @RequestMapping(value = "/ok")
    public String ok(HttpServletRequest request, Model model,Integer msg) {
        return "/ok";
    }
    /**
	 * 后台管理员登录
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/adminLogin")
	public void login(String account,String password, Model model,
			HttpServletRequest request, HttpServletResponse rsp) {
		if(!StringUtil.isEmpty(account)){
			account = account.trim();
		}else{
			responsePrintResult(rsp,3);
		}
		if(!StringUtil.isEmpty(password)){
			password = password.trim();
		}else{
			responsePrintResult(rsp,3);
		}
		account = account.trim();
		password= password.trim();	
		Distributor distributor =	welcomeService.login(account, password);
		if(distributor != null){
			if(distributor.getStatus()!=2){
				//账号已被冻结
				responsePrintResult(rsp,4);
			};
			request.getSession().setAttribute("distributor", distributor);
			model.addAttribute("distributor", distributor);
			responsePrintResult(rsp,1);
		}else{
			responsePrintResult(rsp,2);
		}
		
	}
	
	
	 /**
		 * 后台管理员注册
		 * 
		 * @param model
		 * @param request
		 * @return
		 */
		@RequestMapping(method = RequestMethod.POST, value = "/adminRegister")
		public void adminRegister(String account,String pwd,String name,String phone,String identity,String bank_num, 
				String bank_addr,String address,Model model,HttpServletRequest request, HttpServletResponse rsp) {
			boolean flag = true;
			if(!StringUtil.isEmpty(account)){
				account = account.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(pwd)){
				pwd = pwd.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(name)){
				name = name.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(phone)){
				phone = phone.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(identity)){
				identity = identity.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(bank_addr)){
				bank_addr = bank_addr.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
			if(!StringUtil.isEmpty(address)){
				address = address.trim();
			}else{
				responsePrintResult(rsp,2);
				flag = false;
			}
				
			if(flag){
				Distributor distributor = welcomeService.getDistributorByAccount(account);
				//账号没有重复
				if(distributor == null){
					int result  =	welcomeService.register(account, pwd, name, phone, identity, bank_num,  bank_addr, address);
					if(result>0){
						responsePrintResult(rsp,1);
					}else{
						responsePrintResult(rsp,4);
					}
				}else{
					responsePrintResult(rsp,3);
				}
				
			}
			
		
		}
		
		/**
		 * 异步请求返回结果
		 * 
		 * @param request
		 * @param result
		 */
		protected void responsePrintResult(HttpServletResponse response,
				Object result) {
			try {
				response.setCharacterEncoding("utf-8");
				response.setHeader("Cache-Control", "no-cache");
				response.getWriter().print(result);
				response.getWriter().flush();
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

}
